package com.xdxc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xdxc.feign.QuestionFeign;
import com.xdxc.model.dto.BankQuestionDTO;
import com.xdxc.model.vo.BankQuestionVo;
import com.xdxc.response.Result;
import com.xdxc.utils.TokenManager;
import com.xdxc.vo.QuestionWithOptionsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;


@RestController
@RequestMapping("/train-question")
public class TrainingController {

    @Autowired
    private QuestionFeign questionFeign;

    @Autowired
    private TokenManager tokenManager;


    @GetMapping("/start-train/{bankId}")
    public Result getTrain(@PathVariable Long bankId) {
        String token= tokenManager.getToken();

        Result<List<BankQuestionVo>> result = questionFeign.getBankQuestion("Bearer "+token,1,60,bankId);
        if(result.getCode()!=1){
            return Result.error(result.getMsg());
        }

        List<BankQuestionDTO> bankQuestionDTOList=result.getData().stream()
                .map(bankQuestionVo -> {
                    BankQuestionDTO bankQuestionDTO=new BankQuestionDTO();
                    bankQuestionDTO.setQuestionId(bankQuestionVo.getQuestionId());
                    bankQuestionDTO.setId(bankQuestionVo.getId());
                    bankQuestionDTO.setBankId(bankQuestionVo.getBankId());
                    return bankQuestionDTO;
                })
                .toList();

      Result result1=     Result.success(bankQuestionDTOList);

        result1.setMap(result.getMap());
        return result1;
    }
    @GetMapping("/pageMore")
    public Result TrainPageMore(@RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "6") Integer pageSize,
                                 @RequestParam  Long bankId
    ){
        String token= tokenManager.getToken();

        Result<List<BankQuestionVo>> result = questionFeign.getBankQuestion("Bearer "+token,pageNum,pageSize,bankId);
        if(result.getCode()!=1){
            return Result.error(result.getMsg());
        }

        List<BankQuestionDTO> bankQuestionDTOList=result.getData().stream()
                .map(bankQuestionVo -> {
                    BankQuestionDTO bankQuestionDTO=new BankQuestionDTO();
                    bankQuestionDTO.setQuestionId(bankQuestionVo.getQuestionId());
                    bankQuestionDTO.setId(bankQuestionVo.getId());
                    bankQuestionDTO.setBankId(bankQuestionVo.getBankId());
                    return bankQuestionDTO;
                })
                .toList();

        Result result1= Result.success(bankQuestionDTOList);

        result1.setMap(result.getMap());
        return result1;
    }



    @GetMapping("/question/{questionId}")
    public Result getQuestion(@PathVariable Long questionId){

       String token= tokenManager.getToken();
        Result<QuestionWithOptionsVo> questionWithOptionsVoResult=   questionFeign.getQuestion("Bearer "+token,questionId);
        return questionWithOptionsVoResult;
    }

    @GetMapping("/question/questionBank")
    public Result questionBank(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "6") Integer pageSize){
        String token= tokenManager.getToken();
        Result<QuestionWithOptionsVo> questionWithOptionsVoResult=   questionFeign.getQuestion("Bearer "+token,
                pageNum,
                pageSize);
        return questionWithOptionsVoResult;
    }






}
